package com.cq.controller;

import com.cq.pojo.Meeting;
import com.cq.pojo.Result;
import com.cq.pojo.Room;
import com.cq.service.MeetingService;
import com.cq.service.RoomService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.LinkedList;
import java.util.List;

/**
 * @Author: 悠久的苍穹
 * @Date: 2021/1/27 17:01
 * @Description: 房间控制层
 * @Version: 1.0
 */
@Controller
public class RoomController {

    @Autowired
    RoomService roomService;

    @Autowired
    MeetingService meetingService;

    @ResponseBody
    @GetMapping("/getRoomList")
    public Result getRoomList(@RequestParam int page,
                              @RequestParam int limit) {
        Result result = new Result();
        int count = roomService.getCount();
        List<Room> roomList= roomService.findAllRoom(page, limit);
        result.setCount(count);
        result.setData(roomList);
        result.setCode(0);
        return result;
    }
    @GetMapping("/rtmp/{roomid}")
    public String rtmp(Model model, @PathVariable("roomid") String id){
        Room room = roomService.findRoomById(id);
        model.addAttribute("room",room);
        Meeting meeting = meetingService.findMeetingByRoomId(Integer.parseInt(id));
        model.addAttribute("meeting",meeting);
        return "rtmp/rtmp";
    }

    @GetMapping("/getRoomById")
    @ResponseBody
    public Result getRoomById(@RequestParam String roomid) {
        Result result = new Result();
        Room room = roomService.findRoomById(roomid);
        if (room == null){
            result.setCount(0);
        }else {
            result.setCount(1);
            List<Room> list = new LinkedList<>();
            list.add(room);
            result.setData(list);
        }
        result.setCode(0);
        return result;
    }

    @DeleteMapping("/delRoom/{id}")
    @ResponseBody
    public Result delRoom(@PathVariable("id") String id) {
        Result result = new Result();
        String[] s = id.split(",");
        for (int i = 0; i < s.length; i++) {
            roomService.deleteRoomById(Integer.parseInt(s[i]));
        }
        result.setCode(200);
        return result;
    }

    @GetMapping("/webrtc/{roomid}")
    public String webrtc(Model model, @PathVariable("roomid") String id){
        Room room = roomService.findRoomById(id);
        model.addAttribute("room",room);
        model.addAttribute("a","管理员");
        return "rtmp/webrtc/index";
    }

    @ResponseBody
    @GetMapping("/getWebRoomList")
    public Result getWebRoomList(@RequestParam int page,
                              @RequestParam int limit) {
        Result result = new Result();
        int count = roomService.getWebCount();
        List<Room> roomList= roomService.findAllWebRoom(page, limit);
        result.setCount(count);
        result.setData(roomList);
        result.setCode(0);
        return result;
    }
}
